package com.neuronbit.businessflow.intellij.model;

import lombok.Getter;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;
import java.util.Objects;

/**
 * The type Param value.
 */
@Getter
public class ParamValue implements Serializable {
    private final String value;
    private String source;
    private String[] fields;

    /**
     * Instantiates a new Param value.
     *
     * @param value the value
     */
    public ParamValue(String value) {
        this.value = value;
        if (StringUtils.isEmpty(value)) {
            return;
        }
        String[] split = value.split("\\.");
        if (split.length >= 2) {
            source = split[0];
            fields = ArrayUtils.subarray(split, 1, split.length);
        } else {
            throw new IllegalArgumentException("value of param is not valid");
        }
    }

    @Override
    public String toString() {
        if (null != source && null != fields) {
            return source + "." + String.join(".", fields);
        } else {
            return "";
        }
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        ParamValue that = (ParamValue) o;
        return Objects.equals(value, that.value);
    }

    @Override
    public int hashCode() {
        return Objects.hash(value);
    }
}
